﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using OOAD_PROJEKAT;

namespace DAL
{
    partial class DAL
    {
        public class videoDAO : IDaoCrud<video>
        {

            protected MySqlCommand c;

            public long create(video v)
            {
                try
                {

                    c = new MySqlCommand();

                    
 
                    c.CommandText = "insert into snimci values (@idSnimke, @datum, @snimka)";
                    c.Parameters.AddWithValue("@idSnimke", "");
                    c.Parameters.AddWithValue("@datum", DateTime.Now);
                    c.Parameters.AddWithValue("@snimka", v.snimak.ToArray());

                    c.Connection = con;
                    c.ExecuteNonQuery();

                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<video> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from snimci where 1", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<video> snimci = new List<video>();
                    while (r.Read())
                        snimci.Add(new video(r.GetInt32("idSnimke"), r.GetDateTime("datum"), new List<byte>()));

                    r.Close();
                    return snimci;
                }
                catch (Exception e)
                {
                    
                    throw e;
                }
            }





            public video read(video v)
            {
                try
                {
                    //ne znam da li bi ovdje trebalo sve kolone ispitati jesu li jednake
                    string commandStr = "select * from snimci where idSnimke=" + v.id;
                    c = new MySqlCommand(commandStr, con);
                    MySqlDataReader r = c.ExecuteReader();
                    video pr = null;
                    if (r.Read())
                    {
                        int vel = 90000000;
                        byte[] myBytes = new byte[vel];
                        long vratio = r.GetBytes(r.GetOrdinal("snimka"), 0, myBytes, 0, vel);

                        List<byte> pp = myBytes.ToList<byte>();
                        pp.RemoveRange((int)vratio, vel - (int)vratio - 1);

                        pr = new video(0, r.GetDateTime("datum"), pp);
                    }
                    r.Close();
                    return pr;
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            public video update(video z)
            {
                throw new NotImplementedException();
                /*
                try
                {
                    String commandStr = "update zaposlenici set ime = ?ime, ";
                    commandStr += "prezime = ?prezime, jmbg = ?jmbg, password = ?password, ";
                    commandStr += "plata = ?plata, funkcija = ?funkcija, idFilijale = ?idFilijale ";
                    commandStr += "where idZaposlenika=" + z.Id + " and izbrisan=0";

                    c = new MySqlCommand(commandStr, con);
                    c.Parameters.AddWithValue("?ime", z.Ime);
                    c.Parameters.AddWithValue("?prezime", z.Prezime);
                    c.Parameters.AddWithValue("?jmbg", z.Jmbg);
                    c.Parameters.AddWithValue("?password", z.Password);
                    c.Parameters.AddWithValue("?plata", z.Plata);
                    c.Parameters.AddWithValue("?funkcija", z.Funkcija);
                    c.Parameters.AddWithValue("?idFilijale", z.IdFilijale);

                    c.ExecuteNonQuery();
                    return read(z);
                }
                catch (Exception e)
                {
                    throw e;
                }*/
            }

            public void delete(video z)//brisanje postavlja kolonu "izbrisan" na 1
            {
                throw new NotImplementedException();
                /*
                try
                {
                    string commandStr = "update zaposlenici set izbrisan=1 where idZaposlenika=" + z.Id;
                    c = new MySqlCommand(commandStr, con);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }*/

            }

            public video getById(int id)
            {
                throw new NotImplementedException();
                /*
                try
                {
                    OZaposlenik z = new OZaposlenik(id, "", "", "", "", 0, 0, "");
                    return read(z);
                }
                catch (Exception e)
                {
                    throw e;
                }*/
            }

            public List<video> getByExample(string name, string value)
            {
                throw new NotImplementedException();
                /*
                try
                {
                    c = new MySqlCommand("select * from zaposlenici where izbrisan=0 and " + name + "= '" + value + "'", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<OZaposlenik> zaposlenici = new List<OZaposlenik>();
                    while (r.Read())
                        zaposlenici.Add(new OZaposlenik(r.GetInt32("idZaposlenika"), r.GetString("ime"), r.GetString("prezime"),
                                   r.GetString("jmbg"), r.GetString("password"), r.GetDouble("plata"), r.GetInt32("idFilijale"), r.GetString("funkcija")));

                    r.Close();
                    return zaposlenici;
                }
                catch (Exception e)
                {
                    throw e;
                }*/
            }
            //kada zelimo dobiti sve zaposlenike zadane po parametru funkcija, koji zadovoljavaju name= value;
            

        }
    }
}